package org.dragonnova.meetingapp.app.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;

import org.dragonnova.meetingapp.R;
import org.dragonnova.meetingapp.utils.Constants;
import org.dragonnova.meetingapp.utils.LogUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class SplashActivity extends AppCompatActivity {

    private static String TAG = SplashActivity.class.getName();

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                startActivity(new Intent(SplashActivity.this, SetWifiActivity.class));
                finish();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        handler.sendEmptyMessageDelayed(0, 2500);
        initData();
    }

    /**
     * 初始化应用信息
     */
    private void initData() {
        isFirstLaunch();
    }

    /**
     * 判断是否是第一次登陆，如果是第一次，那么会有引导界面和数据的操作
     */
    private void isFirstLaunch() {
        boolean isFirstLaunch = getSharedPreferences(Constants.APP_GLOBAL_CONFIG, Context.MODE_PRIVATE)
                .getBoolean(Constants.IS_FIRST_LAUNCH, true);
        if (isFirstLaunch) {
            getSharedPreferences(Constants.APP_GLOBAL_CONFIG, Context.MODE_PRIVATE).edit()
                    .putBoolean(Constants.IS_FIRST_LAUNCH, false).commit();
            copyResource();
        }
    }

    /**
     * 复制数据库
     */
    private void copyResource() {
        File parentPath = this.getFilesDir().getAbsoluteFile().getParentFile();
        File databasesDir = new File(parentPath, "databases");
        boolean success = true;
        if (!databasesDir.exists()) {
            success = databasesDir.mkdirs();
        }
        if (success) {
            File file = new File(databasesDir, "userdata");
            if (file.exists()) {
                return;
            }
            InputStream in = null;
            FileOutputStream out = null;
            try {
                in = getResources().openRawResource(R.raw.userdata);
                out = new FileOutputStream(file);
                int length;
                byte[] buf = new byte[1024];
                while ((length = in.read(buf)) != -1) {
                    out.write(buf, 0, length);
                }
                out.flush();
            } catch (IOException e) {
                LogUtil.e("failed to copy databases to the app's directory");
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            LogUtil.e("failed to make databases directory");
        }
    }
}

